package com.roncoo.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Ordering;
import com.roncoo.order.dao.OrderInfoDao;
import com.roncoo.order.dao.UserCourseMapper;
import com.roncoo.order.entity.EduCourse;
import com.roncoo.order.entity.OrderInfo;
import com.roncoo.order.entity.User;
import com.roncoo.order.entity.UserCourse;
import com.roncoo.order.service.OrderInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * (OrderInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-03-24 18:40:22
 */
@Service("orderInfoService")
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoDao, OrderInfo> implements OrderInfoService {

    @Resource
    OrderInfoDao orderInfoDao;
    @Resource
    private UserCourseMapper userCourseMapper;
    @Override
    public IPage<OrderInfo> queryAll(Page page, OrderInfo orderInfo, User user, EduCourse eduCourse) {
        return orderInfoDao.queryOrder(page,orderInfo,user,eduCourse);
    }

    @Override
    public IPage<OrderInfo> findAllById(Page page, OrderInfo orderInfo, User user, EduCourse eduCourse) {
        return orderInfoDao.findAllById(page,orderInfo,user,eduCourse);
    }

    /**
     * 返回自增主键
     * @param orderInfo
     * @return
     */
    @Override
    public int getId(OrderInfo orderInfo) {
        //订单号
        //orderInfo.setOrderNo(String.valueOf(System.currentTimeMillis()));
        //创建时间
        orderInfo.setOrderCreate(LocalDate.now());
        /*System.out.println(orderInfo);*/
        //int i= orderInfoDao.getId(orderInfo);
        //添加数据
        this.save(orderInfo);
        //根据订单号返回主键
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderInfo.getOrderNo());
        OrderInfo one = this.getOne(queryWrapper);
        //主键
        Integer orderId = one.getOrderId();
        return orderId;
    }

    @Override
    public int updStatusAndAdd(int oid) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        //修改订单状态
        queryWrapper.eq("order_id",oid);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus("1");
        orderInfo.setPayTime(LocalDate.now());//修改时间
        boolean update = this.update(orderInfo, queryWrapper);
        //把购买的课程添加到用户课程表中
        OrderInfo byId = this.getById(oid);
        UserCourse userCourse = new UserCourse();
        userCourse.setUserId(byId.getUserNo());
        userCourse.setCourseId(byId.getCourseId());
        userCourse.setCourseName(byId.getCourseName());
        //添加
        int insert = userCourseMapper.insert(userCourse);
        return insert;
    }

    /**
     * 判断用户是否有未支付订单
     * @param uid
     * @param cid
     * @return
     */
    @Override
    public boolean getOneOrder(Integer uid, Integer cid) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_no",uid);
        queryWrapper.eq("course_id",cid);
        queryWrapper.eq("order_status",0);
        OrderInfo one = this.getOne(queryWrapper);
        System.out.println(one);
        if (one==null){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 查询订单
     * @param id
     * @param page
     * @return
     */
    @Override
    public IPage<List<Map>> findAllBylecturer_id(Integer id, Page page) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lecturer_id", id);
        Page page1 = orderInfoDao.selectPage1(page, queryWrapper);
        System.out.println(page1.getRecords());
        return page1;
    }
}
